// packageI/shareGet/shareGet.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mark_id: '',
    getSucceed: false,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.k) {
      this.data.mark_id = options.k
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      // 扫码进入
      if (scene) {
        let info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == 'mark_id') {
            this.setData({
              mark_id: chil_arr[1]
            });
          }
          if (chil_arr[0] == 'mid') {
            app._setMid(chil_arr[1]);
          }
        }
      }
    }
    this.getData();
    wx.setNavigationBarTitle({
      title: '优惠券分享',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var value = wx.getStorageSync("yz_uid");
    let name = wx.getStorageSync('nickname') ? wx.getStorageSync('nickname') : ''
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      title: `${name}向您分享${this.data.item?this.data.item.name:'优惠券'}`,
      path: `/packageI/shareGet/shareGet?k=${this.data.mark_id}&mid=${mid}`
    };
  },
  onShareTimeline() {
    var value = wx.getStorageSync("yz_uid");
    let name = wx.getStorageSync('nickname') ? wx.getStorageSync('nickname') : ''
    var mid = "";
    if (value) {
      mid = value;
    }

    return {
      title: `${name}向您分享${this.data.item?this.data.item.name:'优惠券'}`,
      path: `/packageI/shareGet/shareGet?k=${this.data.mark_id}&mid=${mid}`
    };
  },
  goMyCoupon() {
    wx.redirectTo({
      url: '/packageA/member/coupon_v2/coupon_v2',
    })
  },
  goConponCenter() {
    wx.redirectTo({
      url: '/packageD/coupon/coupon_store',
    })
  },
  tapGet() {
    let json = {
      mark_id: this.data.mark_id,
    }
    let urlStr = app.getNetAddresss("plugin.share-coupons.Frontend.controllers.share.handle");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            getSucceed: true
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    let json = {
      mark_id: this.data.mark_id,
    }
    let urlStr = app.getNetAddresss("plugin.share-coupons.Frontend.controllers.share.index");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            item: res.data
          })
        } else {
          if (res.data.used == 1) {
            wx.showToast({
              title: '优惠券已被领取完',
              icon: 'none',
              duration: 1000,
              success: (res) => {
                setTimeout(() => {
                  wx.redirectTo({
                    url: '/packageA/member/coupon_v2/coupon_v2',
                  })
                }, 1000);
              }
            })
          } else {
            app.tips(res.msg)
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
})